package service;

import dao.GoodsDao;
import model.Goods;
import model.Page;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

public class GoodsService {

    //获取热销商品
    private GoodsDao gDao=new GoodsDao();
    public List<Map<String,Object>> getHotGoodsList(){
        List<Map<String, Object>> list=null;
        try {
            list = gDao.getGoodsList(2);
            System.out.println("service:"+list);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return list;
    }

    //获取新商品
    public List<Map<String,Object>> getNewGoodsList(){
        List<Map<String, Object>> list=null;
        try {
            list = gDao.getGoodsList(2);
            System.out.println("service:"+list);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }
    //获得条幅商品
    public Map<String,Object> getScrollGoods(){
        Map<String, Object> map=null;
        try {
            map = gDao.getScrollGoods();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return map;
    }

    //获得不同类型的商品
    public Page getGoodsPage(int typeId, int pageNo) {
        Page p = new Page();
        p.setPageNo(pageNo);
        int totalCount = 0;
        try {
            totalCount = gDao.getGoodsCount(typeId);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        p.setPageSizeAndTotalCount(8, totalCount);
        List list=null;
        try {
            list = gDao.selectGoods(typeId, pageNo, 8);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        p.setList(list);
        return p;
    }
//获得推荐热销商品的页面
    public Page getGoodsRecommendPage(int type,int pageNo) {
        Page p = new Page();
        p.setPageNo(pageNo);
        int totalCount = 0;
        try {
            totalCount = gDao.selectGoodsRecommendCount(type);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        p.setPageSizeAndTotalCount(8, totalCount);
        List list=null;
        try {
            list = gDao.selectGoodsRecommend(type, pageNo, 8);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        p.setList(list);
        return p;
    }

    //通过id查找商品
    public Goods getById(int id){
        Goods g  = null;
        try {
             g = gDao.getById(id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return g;
    }
}
